<template>
  <div class="login-container">
    <header class="header">
    </header>
    <main class="main">
      <div class="form-box">
        <div class="toggle-buttons">
          <button :class="{ active: isLogin }" @click="toggleForm('login')">登录</button>
          <button :class="{ active: !isLogin }" @click="toggleForm('signup')">注册</button>
        </div>
        <el-form v-if="isLogin" ref="loginForm" :model="user" :rules="loginRules">
          <el-form-item label="账号" prop="username">
            <el-input v-model="user.username" placeholder="请输入账号" />
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="user.password" type="password" placeholder="请输入密码" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleLogin">登录</el-button>
          </el-form-item>
        </el-form>
        <el-form v-else ref="signupForm" :model="user" :rules="signupRules">
          <el-form-item label="账号" prop="username">
            <el-input v-model="user.username" placeholder="请输入账号" />
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="user.password" type="password" placeholder="请输入密码" />
          </el-form-item>
          <el-form-item label="确认密码" prop="confirmPassword">
            <el-input v-model="user.confirmPassword" type="password" placeholder="请再次输入密码" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSignup">注册</el-button>
          </el-form-item>
        </el-form>
      </div>
    </main>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { doPost } from "../../http/httpUserRequest.js";
import { messageTip } from "../../util/util.js";

// 定义页面使用到的变量
let isLogin = ref(true);
let user = reactive({
  username: '',
  password: '',
  confirmPassword: ''
});

// 定义登录表单的验证规则
let loginRules = {
  username: [
    { required: true, message: '请输入账号', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 18, message: '密码长度为6-18位', trigger: 'blur' }
  ]
};

// 定义注册表单的验证规则
let signupRules = {
  username: [
    { required: true, message: '请输入账号', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 18, message: '密码长度为6-18位', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认密码', trigger: 'blur' },
    { validator: checkConfirmPassword, trigger: 'blur' }
  ]
};

// 注册密码验证两次输入是否一样
function checkConfirmPassword(rule, value, callback) {
  if (value !== user.password) {
    callback(new Error('两次输入的密码不一致'));
  } else {
    callback();
  }
};

// 切换注册登录按钮
function toggleForm(formType) {
  isLogin.value = formType === 'login';
};

// 登录函数
function handleLogin() {
  const formName = 'loginForm';
  this.$refs[formName].validate((valid) => {
    if (valid) {
      doPost("/user/login", user).then((resp) => {
        if (resp.data.code === 200) {
          messageTip("登录成功", "success");
          window.location.href = "/dashboard";
        } else {
          messageTip("登录失败", "error");
        }
      });
    }
  });
};

// 注册函数
function handleSignup() {
  const formName = 'signupForm';
  this.$refs[formName].validate((valid) => {
    if (valid) {
      doPost("/user/signin", user).then((resp) => {
        if (resp.data.code === 200) {
          messageTip("注册成功", "success");
          window.location.href = "/login";
        } else {
          messageTip("注册失败", "error");
        }
      });
    }
  });
};
</script>

<style scoped>
.login-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background-color: #f5f5f5;
}

.header {
  margin-bottom: 20px;
  font-size: 30px;
  font-style: italic;
  color: #393939;
}

.main {
  width: 100%;
  max-width: 400px;
}

.form-box {
  background-color: #fff;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

.toggle-buttons {
  margin-bottom: 15px;
  text-align: center;
}

.toggle-buttons span {
  margin: 0 10px;
}

.toggle-buttons button {
  background-color: #f5f5f5;
  border: none;
  font-size: 16px;
  cursor: pointer;
  padding: 5px 15px;
}

.toggle-buttons button.active {
  border-bottom: 2px solid #409EFF;
  color: #409EFF;
}

.el-form-item label {
  font-weight: bold;
}

.el-button {
  width: 100%;
}
</style>
